package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    public SetmealMapper setmealMapper;

    @Autowired
    public SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOneSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal = Setmeal.builder()
                .categoryId(setmealDTO.getCategoryId())
                .description(setmealDTO.getDescription())
                .image(setmealDTO.getImage())
                .name(setmealDTO.getName())
                .price(setmealDTO.getPrice())
                .status(setmealDTO.getStatus())
                .build();

        setmealMapper.addOneSetmeal(setmeal);
//          添加套餐的菜品关系

        setmealDTO.getSetmealDishes().forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.addOneSetmealDish(setmealDish);
        });
    }

    @Override
    public PageResult selectSetmealByPage(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        //2. 执行查询
        List<Setmeal> setmealList = setmealMapper.selectEmpByPage(setmealPageQueryDTO);

        //3. 解析查询结果, 并封装
        Page<Setmeal> p = (Page<Setmeal>) setmealList;
        return new PageResult((int) p.getTotal(), p.getResult());


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSetmealByIds(List<Integer> ids) {

        setmealMapper.deleteSetmealByIds(ids);

        setmealDishMapper.deleteSetmealDishByIds(ids);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SetmealDTO selectSetmealById(Integer id) {
        SetmealDTO setmealDTO = setmealMapper.selectSetmealById(id);

        List<SetmealDish> setmealDishes = setmealDishMapper.selectSetmealDishBySetmealId(id);

        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealMapper.selectSetmealById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal = Setmeal.builder()
                .categoryId(setmealDTO.getCategoryId())
                .description(setmealDTO.getDescription())
                .image(setmealDTO.getImage())
                .name(setmealDTO.getName())
                .price(setmealDTO.getPrice())
                .status(setmealDTO.getStatus())
                .build();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealMapper.updateSetmeal(setmeal);
        List<Integer> ids = new ArrayList<>();
        ids.add(setmealDTO.getId().intValue());
        setmealDishMapper.deleteSetmealDishByIds(ids);
        if(setmealDishes != null && !setmealDishes.isEmpty()){

            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
                setmealDishMapper.addOneSetmealDish(setmealDish);
            });

        }
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        setmealMapper.updateSetmeal(setmeal);

    }
}
